package awk.script.javassist.method04;
//import javassist.*;

import javassist.*;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * @author yujianlong
 * @create 2021-03-19 23:47
 **/
public class MethodDemo {
	public static void main(String[] args) throws NotFoundException, CannotCompileException, IOException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {


		ClassPool classPool = ClassPool.getDefault();

		classPool.importPackage("java.util.List");
		classPool.importPackage("java.util.ArrayList");

		//创建类 用makeClass
		CtClass ctClass = classPool.makeClass("com.yu.test.javassist.create01.testClass06");

		ctClass.setModifiers(Modifier.PUBLIC+Modifier.FINAL);

		//添加属性
		ctClass.addField(CtField.make("private int para = 5;", ctClass));

		//添加setPara方法
		ctClass.addMethod(CtMethod.make("public void setPara(int para){this.para = para;}", ctClass));
		ctClass.addMethod(CtMethod.make("public int getPara(){ return this.para;}", ctClass));



		ctClass.addField(CtField.make("private List/*<String>*/ containers = new ArrayList/*<>*/();", ctClass));

		ctClass.addMethod(CtMethod.make("public List/*<String>*/ getContainers() {\n" +
				"\t\treturn containers;\n" +
				"\t}", ctClass));
		ctClass.addMethod(CtMethod.make("public void setContainers(List/*<String>*/ containers) {\n" +
				"\t\tthis.containers = containers;\n" +
				"\t}", ctClass));

		//添加main方法
		CtMethod mainMethod =CtMethod.make(
				"public static void main(String[] args) {\n" +
						"\t\tSystem.out.println(\"hello,xdf,this my test04\");\n" +
						"\t\tSystem.out.println(\"你好世界\");\n" +
						"\t}",
				ctClass
		);

		//新类添加上方法
		ctClass.addMethod(mainMethod);


		// 创建无参数构造方法
		CtConstructor ctConstructor = new CtConstructor(new CtClass[]{}, ctClass);
		ctConstructor.setBody("{}");
		ctClass.addConstructor(ctConstructor);

		//创建全参构造
		// CtClass.intType, classPool.get("java.sql.Connection" classPool.get("java.lang.String") CtClass.intType
		CtClass[] contructorParameters=new  CtClass[]{CtClass.intType,classPool.get("java.util.List")};
		CtConstructor ctConstructorAllArgs = new CtConstructor(contructorParameters, ctClass);
		ctConstructorAllArgs.setBody("{\n" +
				"\t\t$0.para = $1;\n" +
				"\t\t$0.containers = $2;\n" +
				"\t}");

		ctClass.addConstructor(ctConstructorAllArgs);


		ctClass.writeFile("/Users/yujianlong/Movies/test3/testClass06");




		//获取方法
		CtMethod main2 = ctClass.getDeclaredMethod("main",new  CtClass[]{classPool.get(String[].class.getName())});

		CtMethod getPara2 = ctClass.getDeclaredMethod("getPara",new  CtClass[]{});

		CtMethod setPara2 = ctClass.getDeclaredMethod("setPara",new  CtClass[]{CtClass.intType});

		boolean aStatic = Modifier.isStatic(main2.getModifiers());
		boolean aStaticGetPara2 = Modifier.isStatic(getPara2.getModifiers());

		System.out.println(aStatic);
		System.out.println(aStaticGetPara2);

		boolean aPublic = Modifier.isPublic(getPara2.getModifiers());
		System.out.println(aPublic);

		boolean anInterface = Modifier.isInterface(ctClass.getModifiers());

		System.out.println(anInterface);



	}

}
